#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
@file: apsechedu.py
@time: 2024-07-14 14:30
@desc: 
"""
from datetime import datetime, timedelta
from apscheduler.schedulers.background import BackgroundScheduler
from common_tools import get_logger_with_trace_id
from global_scope import WeChatAccessTokenExpireSeconds
from global_scope import SCHEDULER_LOGGER
from global_scope import WeChatAlreadyPrepared
from wechat.access_token import wechat_access_token_request


# 配置调度器
scheduler = BackgroundScheduler()
scheduler_logger = get_logger_with_trace_id("APScheduler", SCHEDULER_LOGGER)
scheduler_logger.info("flask app already start")


# 定时任务函数
def send_wechat_access_token_request():
    if not WeChatAlreadyPrepared:
        scheduler_logger.info("Wechat Access Token Request Skipped")
        return
    wechat_access_token_request(scheduler_logger)


scheduler.start()
# 两秒之后触发第一次调用
time_object = datetime.now() + timedelta(seconds=2)

# 添加定时任务
scheduler.add_job(
    func=send_wechat_access_token_request,
    trigger="interval",
    next_run_time=time_object,
    seconds=WeChatAccessTokenExpireSeconds)

